package com.changhong.wlyspectrum.NetWork;

import android.util.Log;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;

import okio.Buffer;


/**
 * Created by Mr_tao
 * on 2017/3/8.
 */
public class HttpMethods {
    private static final String TAG = "HttpMethods";
    private static final String SERVER_ADDR = "http://123.57.46.78:18080";
    private static final String APP_KEY = "d0611e59055d00d9c70593c";
    private static final String APP_SECRET = "a8d0611e59055d00d9c70593c";

    //private static final String SERVER_ADDR = "http://chphone.cn:18080";
   // private static final String APP_KEY= "76cb9f8a8d0611e59055d00d9c70593c";
   // private static final String APP_SECRET = "7895e99c8d0611e59055d00d9c70593c";

    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private OkHttpClient mOkHttpClient;
    private static HttpMethods mInstance;

    public HttpMethods() {
        mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
    }

    public static HttpMethods getInstance() {
        if (mInstance == null) {
            synchronized (HttpMethods.class) {
                if (mInstance == null) {
                    mInstance = new HttpMethods();
                }
            }
        }
        return mInstance;
    }

    private Response getResponse1(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(SERVER_ADDR + url)
                .header("appkey", APP_KEY)
                .header("appsecret", APP_SECRET)
                .post(body)
                .build();

        Response response = mOkHttpClient.newCall(request).execute();
        return response;
    }

    public static Response GetResponse(String url, String json) throws IOException {
        return getInstance().getResponse1(url, json);
    }

    private Response newResponse (String url,String id,String token,String json)throws IOException{
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(SERVER_ADDR + url)
                .header("appkey", APP_KEY)
                .header("appsecret", APP_SECRET)
                .header("Authorization", token)
                .header("id", id)
                .post(body)
                .build();

        Response response = mOkHttpClient.newCall(request).execute();
        return response;
    }

    public static Response NewResponse(String url,String id,String token,String json) throws IOException {
        return getInstance().newResponse(url,id,token,json);
    }

    private Response getResponse2(String url, String id, String token) throws IOException {
        Request request = new Request.Builder()
                .url(SERVER_ADDR + url)
                .header("appkey", APP_KEY)
                .header("appsecret", APP_SECRET)
                .header("Authorization", token)
                .header("id", id)
                .build();

        Response response = mOkHttpClient.newCall(request).execute();
        return response;
    }

    public static Response TGetResponse(String url, String id, String token) throws IOException {
        return getInstance().getResponse2(url, id, token);
    }

    private Response updateCollectionData(String url, String token, String id, String name, String description, String attributeTemplate) throws IOException{
        RequestBody body = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addFormDataPart("name",name)
                .addFormDataPart("description",description)
                .addFormDataPart("attributesTemplate",attributeTemplate)
                .build();

        Request request = new Request.Builder()
                .url(SERVER_ADDR + url)
                .header("appkey", APP_KEY)
                .header("appsecret", APP_SECRET)
                .header("Authorization", token)
                .header("id", id)
                .post(body)
                .build();

        Response response = mOkHttpClient.newCall(request).execute();
        return response;
    }

    public static Response UpdateCollectionData(String url, String token, String id, String name, String description, String attributeTemplate) throws IOException{
        return getInstance().updateCollectionData(url,token,id,name,description,attributeTemplate);
    };


    private Response updateResponse(String url, String token, String userId, String deviceId, String name,String collectionId, String description, String sample,String metadata,
                                    String firmwareVersion, String appVersion, String softVersion, String modelVersion) throws IOException {
        RequestBody body = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addFormDataPart("userId", userId)
                .addFormDataPart("deviceId", deviceId)
                .addFormDataPart("name", name)
                .addFormDataPart("collectionId", collectionId)
                .addFormDataPart("description", description)
                .addFormDataPart("isSample", sample)
                .addFormDataPart("metadata", metadata)
                .addFormDataPart("firmwareVersion", firmwareVersion)
                .addFormDataPart("appVersion", appVersion)
                .addFormDataPart("softVersion", softVersion)
                .addFormDataPart("modelVersion", modelVersion)
                .build();
        Request request = new Request.Builder()
                .url(SERVER_ADDR + url)
                .header("appkey", APP_KEY)
                .header("appsecret", APP_SECRET)
                .header("Authorization", token)
                .header("id", userId)
                .addHeader("Content-Type", "multipart/form-data;boundary=----MyFormBoundarySMFEtUYQG6r5B920")
                .post(body)
                .build();

        Response response = mOkHttpClient.newCall(request).execute();
        return response;
    }

    public static Response UpdateResponse(String url, String token, String userId, String deviceId,String name, String collectionId, String description, String sample,String metadata,
                                          String firmwareVersion, String appVersion, String softVersion, String modelVersion) throws IOException {
        return getInstance().updateResponse(url, token, userId, deviceId,name, collectionId, description, sample,metadata, firmwareVersion, appVersion, softVersion, modelVersion);
    }

}
